package game

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/game"
	gameReq "github.com/flipped-aurora/gin-vue-admin/server/model/game/request"
	gameRes "github.com/flipped-aurora/gin-vue-admin/server/model/game/response"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BsGameInfoApi struct{}

// CreateGameInfo
// @Tags      BsGameInfo
// @Summary   创建比赛
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsGameInfo                  true  "api路径, api中文描述, api组, 方法"
// @Success   200   {object}  response.Response{msg=string}  "创建基础api"
// @Router    /api/createGame [post]
func (s *BsGameInfoApi) CreateGameInfo(c *gin.Context) {
	var param game.BsGameInfo
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(param, utils.ApiVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = gameInfoService.CreateGameInfo(param)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteGameInfo
// @Tags      BsGameInfo
// @Summary   删除赛事信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsGameInfo                  true  "Id"
// @Success   200   {object}  response.Response{msg=string}  "删除api"
// @Router    /api/deleteGameInfo [delete]
func (s *BsGameInfoApi) DeleteGameInfo(c *gin.Context) {
	var param game.BsGameInfo
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(param, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = gameInfoService.DeleteGameInfo(param)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// GetGameInfoList
// @Tags      BsGameInfo
// @Summary   分页获取API列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      gameReq.GameInfoParams                               true  "分页获取赛事列表"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页获取API列表,返回包括列表,总数,页码,每页数量"
// @Router    /api/getGameInfoList [post]
func (s *BsGameInfoApi) GetGameInfoList(c *gin.Context) {
	var pageInfo gameReq.GameInfoParams
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	dataScope := common.DataScope{
		UserId:      utils.GetUserID(c),
		AuthorityId: utils.GetUserAuthorityId(c),
	}
	list, total, err := gameInfoService.GetGameInfoList(pageInfo.BsGameInfo, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc, dataScope)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetGameInfoById
// @Tags      BsGameInfo
// @Summary   根据id获取赛事信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.GetById                                   true  "根据id获取赛事信息"
// @Success   200   {object}  response.Response{data=gameRes.BsGameInfoResponse}  "根据id获取api,返回包括api详情"
// @Router    /api/getGameInfoById [get]
func (s *BsGameInfoApi) GetGameInfoById(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	gameInfo, err := gameInfoService.GetGameInfoById(idInfo.Id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(gameRes.BsGameInfoResponse{GameInfo: gameInfo}, "获取成功", c)
}

// UpdateGameInfo
// @Tags      BsGameInfo
// @Summary   修改赛事信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsGameInfo                  true  "api路径, api中文描述, api组, 方法"
// @Success   200   {object}  response.Response{msg=string}  "修改赛事信息"
// @Router    /api/updateGameInfo [put]
func (s *BsGameInfoApi) UpdateGameInfo(c *gin.Context) {
	var param game.BsGameInfo
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(param, utils.ApiVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = gameInfoService.UpdateGameInfo(param)
	if err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
		return
	}
	response.OkWithMessage("修改成功", c)
}
